// Declare a variable to generate unique notification IDs
var notID = 0;
var ETENSION_ID = "lamdojhcdfekgohfpccifgflkbmdcdme";

// Window initialization code. Set up the various event handlers
window.addEventListener("load", function() {
    //Handle Test Button
    $("#btnTest").click(function() {
        HandleTestButton();
    });

    //Handle Clear Button
    $("#btnClear").click(function() {
        HandleClearButton();
    });
});

//Handle Test Button
function HandleTestButton() {
    if (!ValidateData()) {
        $(".error").first().focus();
        return;
    }
    //    ShowNotification();
}

//Handle Clear Button
function HandleClearButton() {
    //clear error
    $(".input").removeClass("error");

    //Clear message
    $("#message").html("");

    $(".input").val("");
    $("#txtTeamOneName").focus();
}


function ValidateData() {
    //clear error
    $(".input").removeClass("error");

    var messageArr = new Array();

    //    CheckRequire("TeamOneName", messageArr);
    //    CheckRequire("TeamTwoName", messageArr);
    CheckSite("A", messageArr);
    //    CheckSite("B", messageArr);

    ShowMessage(messageArr);
    if (messageArr.length == 0) {
        CheckInputSiteValue("A");
    }
    return messageArr.length === 0;
}

function CheckSite(siteName, messageArr) {
    CheckRequire(siteName + "Name", messageArr);
    CheckRequire(siteName + "ValueOne", messageArr);
    //    CheckRequire(siteName + "ValueTwo", messageArr);
}

function CheckInputSiteValue(siteName) {
    var nameLabel = $("#lbl" + siteName + "Name").html();
    var name = $("#txt" + siteName + "Name").val();

    var valueOneLabel = $("#lbl" + siteName + "ValueOne").html();
    var valueOne = $("#txt" + siteName + "ValueOne").val();

    var valueTwoLabel = $("#lbl" + siteName + "ValueTwo").html();
    var valueTwo = $("#txt" + siteName + "ValueTwo").val();

    var data = {
        Name: { Label: nameLabel, Value: name },
        ValueOne: { Label: valueOneLabel, Value: valueOne },
        ValueTwo: { Label: valueTwoLabel, Value: valueTwo }
    };
    chrome.runtime.sendMessage(
              ETENSION_ID,
              { CheckType: "CheckSite",
                  Data: data
              },
              function(response) {
              ShowMessageFromExtension(response.Message);
              }
      );
}

function CheckRequire(itemKey, messageArr) {
    var item = $("#txt" + itemKey);
    if (!$.trim(item.val())) {
        item.addClass("error");
        var label = $("#lbl" + itemKey).html();
        messageArr.push(CM_Function.StringFormat("{0} là bắt buộc nhập.", label));
        return false;
    }
    return true;
}

function ShowMessageFromExtension(message) {
    $("#message").append(message);
}

function ShowMessage(messageArr) {
    //Clear message
    var divMessage = $("#message");
    divMessage.html("");

    if (messageArr.length === 0)
        return;

    var message = "";
    for (var i = 0; i < messageArr.length; i++) {
        message += "<li>" + messageArr[i] + "</li>";
    }

    divMessage.html(message);
}

function ShowNotification() {
    var path = chrome.runtime.getURL("/images/south-america-400x400.png");
    var options = null;
    options = {
        type: "basic",
        title: "Basic Notification",
        message: "Short message part",
        expandedMessage: "Longer part of the message"
    };
    options.iconUrl = path;
    options.buttons = [];
    chrome.notifications.create("id" + notID++, options, creationCallback);
}

function creationCallback(notID) {
    console.log("Succesfully created " + notID + " notification");
}

// Event handlers for the various notification events
function notificationClosed(notID, bByUser) {
    console.log("The notification '" + notID + "' was closed" + (bByUser ? " by the user" : ""));
}

function notificationClicked(notID) {
    console.log("The notification '" + notID + "' was clicked");
}

function notificationBtnClick(notID, iBtn) {
    console.log("The notification '" + notID + "' had button " + iBtn + " clicked");
}